package com.cmnit.analysis.util;

import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件工具类
 *
 * @author chan
 * @version 1.0
 * @since 2022.3.3
 */
public class ConfigurationManager {
    //创建配置对象
    private static Properties prop = new Properties();

    //静态代码块
    static {
        try {
            //通过流读取配置文件
            InputStream inputStream = ConfigurationManager.class.getClassLoader().getResourceAsStream("cluster.properties");

            if (inputStream == null) {
                inputStream = ConfigurationManager.class.getClassLoader().getResourceAsStream("local.properties");
                System.out.println("已获取测试环境的配置文件");
            } else {
                System.out.println("已获取正式环境的配置文件");
            }

            //加载配置文件
            prop.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取配置项
    public static String getProperty(String key) {
        return prop.getProperty(key);
    }

    //获取配置项
    public static String getProperty(String key, String defaultValue) {
        return prop.getProperty(key, defaultValue);
    }

    //获取布尔类型的配置项
    public static boolean getBoolean(String key) {
        String value = prop.getProperty(key);
        try {
            return Boolean.parseBoolean(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
